#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
# File Name: backup.sh
# Author: GID5564
# Description: function for backup
# Created Time: 19/11/23-20:37:57
#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
#! /usr/bin/env bash


# 避免重复导入
#[[ -n $__XLIB_IMPORTED__BACKUP ]] && return 0
#__XLIB_IMPORTED__BACKUP=1

function __backup_init__() {
    #导入函数库
    #import cmd
    #import file
    wait
}

########################################
# 描述: 备份文件，文件名后缀以_bk结尾
# 参数: $1,备份文件的完整目录
# 返回: 
# 实例: bk_file -m xxx.txt
########################################
function bk_file(){
    [[ -z "$@" ]] && {
        error "Usage: bk_file [-c|-m]<file-path> "
        return $INVALID_ARGUMENTS
    }
    local cmd
    local file
    [[ $# -gt 1 ]] && {
        case $1 in
            -c) cmd="cp -vf" ;;
            -m) cmd="mv -vf" ;;
        esac
        file=$2
    } || {
        cmd="cp -vf"
        file=$1
    }
    [[ ! -s "$file" ]] && {
        error "无效文件: ${file}"
        return $INVALID_ARGUMENTS
    }
    local f_path=${file%/*}
    [[ ${f_path} = ${file} ]] && f_path=" " || f_path="${f_path}/"

    local f_name=${file##*/}
    f_name=${f_name%.*}

    local f_suff=${file##*.}

    local temp="${f_path}${f_name}.${f_suff}_bk"

    chmod 777 ${file}
    eval "$cmd ${file} ${temp}"
    unset cmd
    unset file f_path f_name f_suff temp
}


########################################
# 描述: 备份目录
# 参数: $1,tar备份文件的目录，$2,保存目录。缺损:当前目录
# 返回: (备份文件在所设目录下)
# 实例: backup_dir <dir-path> <save-path>
########################################
function backup_dir(){
    [[ -z "$1" || -z "$2" ]] && {
        error "Usage: backup_dir <dir-path> <save-path>"
        return $INVALID_ARGUMENTS
    }
    [[ ! -d "$1" ]] && {
        error "无效备份目录: ${RESET}${YELLOW}${1}${RESET}"
        return $INVALID_ARGUMENTS
    }
    local b_dir=$1
    local b_save=$2
    [[ ! -d "$2" ]] && {
        b_save=$(pwd)
    }
    
    msg "${BLUE}安装所须包…${RESET}"
    install_dpkg_packages "whiptail,tar,pv"


    if ("${WINDOW:-whiptail}" --title "请选择压缩类型" --yes-button "tar.xz" --no-button "tar.gz" --yesno "tar.xz压缩率高，但速度慢。\ntar.gz速度快，但压缩率低。" 10 30); then
		format='xz'
	else
		format='gz'
	fi
	
	local b_name="${b_save}/${b_dir##*/}_$(date +%Y%m%d%H%M).tar.${format}"
	
	msg "${BLUE}将目录[${RESET}${YELLOW}${b_dir}${RESET}${BLUE}]下文件备份至${RESET}${YELLOW}${b_name}${RESET}"
		
	msg "${BLUE}开始备份，${RESET}${YELLOW}如出现部分文件无权限属于正常现象...${RESET}"
    press
    if [[ "${format}" == "xz" ]]; then
		tar --exclude=.git -PJpcf - ${b_dir} | (pv -p --timer --rate --bytes --numeric > ${b_name})
	else
		tar --exclude=.git -Ppczf - ${b_dir} | (pv -p --timer --rate --bytes --numeric > ${b_name})
	fi
	

	ls -lth ${b_save}/${b_dir##*/}_*.tar.*
	success "备份完成!"
	unset b_dir
    unset b_save
    unset b_name
}

########################################
# 描述: 恢复目录下文件，即还原文件，须tar压缩文件
# 参数: $1,tar压缩文件
# 返回: (恢复目录下原文件)
# 实例: recover_file [tar file]
########################################
function recover_file(){
    [[ -z "$1" ]] && {
        error "Usage: recover_file <tar file>"
        return $INVALID_ARGUMENTS
    }
    [[ ! -f "$1" ]] && {
        error "无效备份文件: ${RESET}${YELLOW}${1}${RESET}"        
        return $INVALID_ARGUMENTS
    }
    local r_file=$1
    
    msg "${BLUE}安装所须包…${RESET}"
    install_dpkg_packages "tar,pv"

    if [[ "${r_file:0-6:6}" == 'tar.xz' ]]; then
		pv ${r_file} | tar -PJx
		success "还原完成!"
	elif [[ "${r_file:0-6:6}" == 'tar.gz' ]]; then
		pv ${r_file} | tar -Pzx
		success "还原完成!"
	else
		error "备份文件[${r_file}]不存在或不合法！"
	fi
	unset r_file
}


########################################
# 描述: 恢复目录文件
# 参数: $1,tar备份文件的目录，缺损:当前目录
# 返回: (恢复目录下备份的文件)
# 实例: recover_backup [dir]
########################################
function recover_backup(){
    [[ -z "$1" ]] && {
        error "Usage: recover_backup <backup-dir-path>"
        return $INVALID_ARGUMENTS
    }
    local f_path=$1
    [[ ! -d $1 ]] && f_path=$(pwd)
    
    local file=()
    local r_file
    local num
    local i
    local f_num=0
    
    msg "${BLUE}目录[${RESET}${YELLOW}${f_path}${RESET}${BLUE}]下备份文件列表，请选择文件：${RESET}"
    SpecChar '\n'
    
    f_num=$(file_count ${f_path})
    if [[ ${f_num} -ne 0 ]];then
	    i=0
	    for loop in ${f_path}/*.*
	    do
		    i=`expr $i + 1`
		    msg "   [${RED}${i}${RESET}]   ${YELLOW}${loop##*/}${RESET}"
		    file[${i}]=${loop}
	    done
	    SpecChar '\n'
	    msg "${BLUE}共识别到文件数:${RESET} ${RED}${#file[@]}${RESET}"
	
	    Msg "${BLUE}请输入序号: ${RESET}"
	    read -k num
	    SpecChar '\n'
	    if [[ ${num} -le ${#file[@]} ]]; then
	        recover_file ${file[${num}]}
        else
            error "输入错误!"
	    fi
	fi
	unset f_path
    unset file
    unset r_file
    unset num
    unset i
    unset f_num
}





















__backup_init__

